package com.dekarrin.scraton.test;

import com.dekarrin.scraton.*;

/**
 * Tests the output script functionality of the System. Outputs a test sript to
 * a file. All options of formatting are configurable.
 */
public class OutputScript {

	public static void main(String[] args) {
		new OutputScript(args);
	}
	
	public OutputScript(String[] args) {
		ScriptFormat format = getFormatOptions(args);
		String outputFile = args[7];
		Script theScript = createScript(format);
		writeScript(theScript, outputFile);
	}
	
	private void writeScript(Script s, String outputFile) {
		try {
			java.io.BufferedWriter out = new java.io.BufferedWriter(new java.io.FileWriter(outputFile));
			out.write(s.toString());
			out.close();
		} catch(Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
	}
	
	private ScriptFormat getFormatOptions(String[] args) {
		if (args.length < 8) {
			System.err.println("Not enough arguments.\n");
			showSyntax();
			System.exit(1);
		}
		ScriptFormat f = new ScriptFormat();
		try {
			f.scriptWidth = Integer.parseInt(args[0]);
			f.leftMarginWidth = Integer.parseInt(args[1]);
			f.rightMarginWidth = Integer.parseInt(args[2]);
			f.useQuotes = Boolean.parseBoolean(args[3]);
			f.speakerInMargin = Boolean.parseBoolean(args[4]);
			f.authorLength = NameLength.valueOf(args[5]);
			f.indentActions = Boolean.parseBoolean(args[6]);
		} catch(Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
		return f;
	}
	
	private void showSyntax() {
		System.err.println("Arguments are as follows:");
		System.err.println("scriptWidth, leftMarginWidth, rightMarginWidth, useQuotes,");
		System.err.println("speakerInMargin, authorLength, indentActions, output-file");
	}
	
	private Script createScript(ScriptFormat f) {
		Author jack = new Author("jackson jelly jammin julesy", Gender.MALE);
		Author troll = new Author("troll mctroll", Gender.FEMALE);
		Script s = new Script();
		s.addInstruction(new TransitionLine(-1, f, "Fade in", true));
		s.addInstruction(new SlugLine(-1, f, InsideOutside.INTERIOR, "jack's house - living room", "day"));
		s.addInstruction(new ActionLine(-1, f, "We see JACK sitting in his living room. He is watching TV. Noises from the TV indicate that the FOOTBALL ANNOUNCER is saying words. JACK wants to listen to the words, so he turns up the TV. Now the TV is very loud."));
		s.addInstruction(new ActionLine(-1, f, "CLOSE ON Jack's face. Jack scrunches up his face."));
		s.addInstruction(new DialogLine(-1, f, "I think I'll go on the internet.", jack, null));
		s.addInstruction(new TransitionLine(-1, f, "Cut to", true));
		s.addInstruction(new SlugLine(-1, f, InsideOutside.INTERIOR, "Troll cave", "same"));
		s.addInstruction(new ActionLine(-1, f, "We see THE TROLL at her computer, about to upload a video on to YouTube."));
		s.addInstruction(new ActionLine(-1, f, "OVER SHOULDER of Troll. We see her uploading a video."));
		s.addInstruction(new ActionLine(-1, f, "CLOSE ON Troll's face."));
		s.addInstruction(new DialogLine(-1, f, "Now everyone will fall prey to my trickery, and I shall laugh at them until they can no longer be laughed at! I will be the most ultimate troll ever!", troll, new String[]{"darkly"}));
		s.addInstruction(new TransitionLine(-1, f, "Back to", false));
		s.addInstruction(new SlugLine(-1, f, InsideOutside.INTERIOR, "jack's house - study", "same"));
		s.addInstruction(new ActionLine(-1, f, "Jack is about to click on a link on YouTube."));
		s.addInstruction(new DialogLine(-1, f, "Oh look! HL3 has come out. I shall observe this excellent video.", jack, null));
		s.addInstruction(new ActionLine(-1, f, "Jack clicks the link."));
		s.addInstruction(new ScoreLine(-1, f, "Rick Astley", "Never Gonna Give You Up"));
		s.addInstruction(new ActionLine(-1, f, "CLOSE ON Jack's face. He's pissed."));
		s.addInstruction(new DialogLine(-1, f, "Noooooooooooo!", jack, null));
		s.addInstruction(new TransitionLine(-1, f, "fade to black", true));
		return s;
	}
	
}